﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;
using System.Web.Script.Serialization;


namespace CrmApplication.Controllers
{
    public class CampaignController : Controller
    {
        //
        // GET: /CampaignModels/

        public ActionResult Index()
        {
            return View();
        }

		/// <summary>
		/// get all campaigns
		/// </summary>
		/// <returns></returns>
		public ActionResult GetCampaign ()
		{
			string searchCampaign = Request.Params["query"];
			CampaignContainer container = CampaignModels.GetCampaignArray(searchCampaign);
			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// get all campaigns and nr of contacts
		/// </summary>
		/// <returns></returns>
		public ActionResult GetCampaignCount ()
		{
			CampaignContainer container = CampaignModels.GetCampaignCountArray();
			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// add new campaign
		/// </summary>
		/// <returns></returns>
		public ActionResult AddCampaign ()
		{
			string Campaign = Request.Params["data"];

			if (Campaign != "")
			{
				CampaignModels.AddCampaign(Campaign);
			}

			return Json(new { success = true, message = "some message" });
		}

		/// <summary>
		/// move contact from one campaign to another
		/// </summary>
		/// <returns></returns>
		public ActionResult MoveDeleteContact ()
		{
			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["id_campaign"],out idCampaign);
			decimal op = 0;
			Decimal.TryParse(Request.Params["op"], out op);

			CampaignModels campaign = new CampaignModels();
			campaign.Id = idCampaign;
			
			//JavaScriptSerializer serializer = new JavaScriptSerializer();
			//List<CampaignModels> contactIds = (List<CampaignModels>)serializer.Deserialize(contactSt, typeof(List<CampaignModels>));

			string contactSt= Request.Params["contacts"];
			string messageBack = null;

			string[] contactIds = new string[contactSt.Length];
			contactIds = contactSt.Split(new char[] { '[',',' ,']'},StringSplitOptions.RemoveEmptyEntries);

			decimal[] contactIdsD = new decimal[contactIds.Length];
			for (int i = 0; i < contactIds.Length; i++)
			{
				contactIdsD[i] = Decimal.Parse(contactIds[i]);
			}

			try
			{
				if (op == 1)
				{
					for (int i = 0; i < contactIdsD.Length; i++)
					{
						campaign.IdContact = contactIdsD[i];
						if (CampaignModels.CheckContactCampaign(campaign) == null)
						{
							CampaignModels.MoveContactCampaign(campaign);
						}
					}
					messageBack = "Contactele au fost adaugate cu succes!";
				}
				else
				{
					if (op == 0)
					{
						for (int i = 0; i < contactIdsD.Length; i++)
						{
							campaign.IdContact = contactIdsD[i];
							CampaignModels.DeleteContactCampaign(campaign);
						}
						messageBack = "Contactele au fost sterse cu succes!";
					}
				}
				return Json(new { success = true, description = messageBack });
			}
			catch (Exception ex)
			{
				return null;
			}			
		}

    }
}
